<?php
namespace Admin\Model;
use Think\Model;
class MemberModel extends Model{
	protected $_validate = array(
		array('username','require','帐号必须填写！'), 
		array('password','require','密码必须填写！'),     		
	);
	
	public function login(){
		$verify   = trim(I('verify'));
		$username = trim(I('username'));
		$password = trim(I('password'));
		if(!$this -> check_verify($verify)){
			return 0;//验证码错误
			exit;
		}else{
			//$pass = md5(md5($password).'jiangchengzi');
			$pass = mdPass($password);
			$member = $this -> where(array('username'=>$username,'password'=>$pass)) -> find();
			if(!$member){
				return 1;//帐号或密码错误！请核实后登录
				exit;
			}
			if($member['lock'] == 0){
				return 2;//用户已被锁定
				exit;
			}
			
			//保存登录信息
			$loginlast = $this -> where(array('username' => $username)) -> find();
			$datalast['loginlast'] = $loginlast['logintime'];
			$this -> where(array('username' => $username)) -> save($datalast);//修改最后一次登录时间
			$data = array(
				'id' => $member['id'],
				'loginip' => get_client_ip(),
				'logintime' => time()
				);

			$this -> where('id='.$member['id'])->save($data);//保存登录信息
			$this -> where(array('username' => $username)) -> setInc('loginnum');//登录次数
			
			//存入SESSION
			$_SESSION['session_username'] = $member['username'];
			$_SESSION['session_id'] = $member['id'];


			//超级管理员识别
			if(C('ADMIN_AUTH_KEY') == $_SESSION['session_username']){
				$_SESSION[C('ADMIN_AUTH_KEY')] = true;
			}else{

				//读取用户权限
				$rbac = new \Org\Util\Rbac();  
		// return $rbac;
		// exit;
				//取出用户权限信息  
				$rbac::saveAccessList($_SESSION['session_id']); 
				//p($_SESSION);die;
			// return $info;
							
			}		
			return 3;
			exit;				
		}			
	}
	
	//验证码校验
	public function check_verify($code, $id = ''){    
		$verify = new \Think\Verify();    
		return $verify->check($code, $id);
	}
	
	
	public function indexDo(){
		$seach =trim(I('seach'));
		if(isset($seach) && $seach!=null){
			  $where['_string'] = "(id like '%".$seach."%')OR(username like '%".$seach."%')";
		}
		$count      = $this->where($where)->count();
		$Page       = new \Think\Page($count,10);
		$show       = $Page->show();
		$member = $this -> where($where)-> order('id desc') -> limit($Page->firstRow.','.$Page->listRows)-> select();
		return array(
			'member' => $member,
			'page' => $show,
		);		
	}
	
	public function addDo(){
		$username = trim(I('username'));
		$password = trim(I('password'));
		$pass     = trim(I('pass'));
		if(false == check_pass($password,$pass)){
			return 0;//两次密码不相同
			exit;
		}
		$data['username'] = $username;
		$data['password'] = mdPass($password);
		$data['loginip']  = get_client_ip();
		$data['logintime']= time();
		$info = $this ->add($data);
		if($info){
			$role = array();
			foreach($_POST['role_id'] as $v){
				$role[] = array(
					'role_id' => $v,
					'user_id' => $info
				);
			}
			M('role_user') -> addAll($role);
		}
		return 1;
	}
	
	public function editDo(){
		$id       = I('id','','intval');
		$username = I('username');
		$password = trim(I('password'));
		$pass     = trim(I('pass'));
		if($password !== '' && $pass !==''){
			if(false !== check_pass($password,$pass)){
				$data['password'] = mdPass($password);
			}else{
				return 0;//两次密码不相同
				exit;	
			}
		}else{
			unset($data['password']);
		}
		//删除原来地 角色
		M('role_user') -> where(array('user_id' => $id)) -> delete();
		//插入新的角色
		$arr = array();
		foreach($_POST['role_id'] as $v){
			$arr[] = array(
				'role_id' => $v,
				'user_id' => $id, 
			);
		}
		//插入角色 用户  信息
		M('role_user') -> addAll($arr);
		$data['id'] = I('id');
		$data['username'] = I('username');
		$data['lock'] = I('lock');
		$this -> data($data) -> save();	
		return 1;
	}
	
}